package com.greenantmall.activity;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.activity.fragment.OrdersListFragment;
import com.greenantmall.adapter.MyFragmentPagerAdapter;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.version.IntentUtil;
import com.lsarah.utils.SystemBarTintManager;

public class MyOrdersActivity extends FragmentActivity {
	public static final String STYLE_ON_SHELF = "OnShelf";
	public static final String STYLE_OFF_SHELF = "OffShelf";
	public static final String STYLE_GOODS_STYLES = "GetGoodsStyles";
	public static final String STYLE_GOODS_PERSTYLE = "GetGoodsPerStyle";
	private static final String TAG = "MyStoreActivity";
	private ViewPager mPager;
	private ArrayList<Fragment> fragmentsList;
	private ImageView ivBottomLine;
	private TextView tvTab1, tvTab2, tvTab3, tvTab4;

	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	private int position_two;
	private int position_three;

	private Resources resources;
	
	private LinearLayout netWork;
	private RelativeLayout noNetWork;
	private boolean canExit = false;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		SystemBarTintManager.setContentView(this, R.layout.myorders, getResources().getColor(R.color.green));
		resources = getResources();
		
		netWork = (LinearLayout) this.findViewById(R.id.netWork);
		noNetWork = (RelativeLayout) this.findViewById(R.id.noNetWork);

		if (IntentUtil.isConnect(this)) {
			netWork.setVisibility(View.VISIBLE);
			noNetWork.setVisibility(View.GONE);
		} else {
			noNetWork.setVisibility(View.VISIBLE);
			netWork.setVisibility(View.GONE);

		}
		
		initWidth();
		initTextView();
		initViewPager();
		registerReciver();
	}
	
	private void registerReciver() {
		IntentFilter downloadManagerFilter = new IntentFilter();
		downloadManagerFilter
				.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
		this.registerReceiver(netManagerReceiver, downloadManagerFilter);
	}
	private BroadcastReceiver netManagerReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// 网络连通
			if (IntentUtil.isConnect(context)) {
				netWork.setVisibility(View.VISIBLE);
				noNetWork.setVisibility(View.GONE);
			} else {
				noNetWork.setVisibility(View.VISIBLE);
				netWork.setVisibility(View.GONE);

			}
		}

	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(netManagerReceiver);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {

		switch (keyCode) {

		case KeyEvent.KEYCODE_SEARCH:
			return true;

		case KeyEvent.KEYCODE_BACK:// 按回退键
			if (noNetWork.getVisibility() == View.VISIBLE) {
				if (canExit) {
					UtilTools.exit(this);
				} else {
					loadDataHandler.sendEmptyMessageDelayed(1002, 2000);
					canExit = true;
					Toast.makeText(this, getString(R.string.main_exit_tip),
							Toast.LENGTH_SHORT).show();
				}

				return true;
			}

		default:
			break;
		}
		return super.onKeyUp(keyCode, event);
	}

	private Handler loadDataHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1002: {
				canExit = false;
				break;
			}
			default:
				break;
			}
		}
	};

	private void initTextView() {
		tvTab1 = (TextView) findViewById(R.id.tv_tab_activity);
		tvTab2 = (TextView) findViewById(R.id.tv_tab_groups);
		tvTab3 = (TextView) findViewById(R.id.tv_tab_friends);
		tvTab4 = (TextView) findViewById(R.id.tv_tab_chat);

		tvTab1.setOnClickListener(new MyOnClickListener(0));
		tvTab2.setOnClickListener(new MyOnClickListener(1));
		tvTab3.setOnClickListener(new MyOnClickListener(2));
		tvTab4.setOnClickListener(new MyOnClickListener(3));
	}

    private void initViewPager()
    {
        mPager = (ViewPager) findViewById(R.id.vPager);
        fragmentsList = new ArrayList<Fragment>();

        String userId=AVUser.getCurrentUser().getObjectId();
        Fragment salingfragment = OrdersListFragment.newInstance(OrdersListFragment.ORDER_TYPE_ALL,userId);
        Fragment offShelfFragment = OrdersListFragment.newInstance(OrdersListFragment.ORDER_TYPE_UNPAY,userId);
        
        Fragment stylesFragment1 = OrdersListFragment.newInstance(
            OrdersListFragment.ORDER_TYPE_UNSEND,userId);//待发货
        
        Fragment stylesFragment = OrdersListFragment.newInstance(
                OrdersListFragment.ORDER_TYPE_UNRECEIVE,userId);//待收货

        fragmentsList.add(salingfragment);
        fragmentsList.add(offShelfFragment);
        fragmentsList.add(stylesFragment1);
        fragmentsList.add(stylesFragment);
        

        mPager.setAdapter(new MyFragmentPagerAdapter(
            getSupportFragmentManager(), fragmentsList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

	private void initWidth() {
		ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
		Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;

		position_one = (int) (screenW / 4.0);

		LayoutParams lay = ivBottomLine.getLayoutParams();
		lay.width = position_one;
		ivBottomLine.setLayoutParams(lay);
		bottomLineWidth = ivBottomLine.getLayoutParams().width;
		offset = (int) ((screenW / 4.0 - bottomLineWidth) / 2);
		Log.i("MyStoreActivity", "offset=" + offset);

		position_two = position_one * 2;
		position_three = position_one * 3;
	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, 0, 0, 0);
					tvTab2.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two, 0, 0, 0);
					tvTab3.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three, 0, 0, 0);
					tvTab4.setTextColor(resources.getColor(R.color.black));
				}
				tvTab1.setTextColor(resources.getColor(R.color.red));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_one, 0, 0);
					tvTab1.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two,
							position_one, 0, 0);
					tvTab3.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three,
							position_one, 0, 0);
					tvTab4.setTextColor(resources.getColor(R.color.black));
				}
				tvTab2.setTextColor(resources.getColor(R.color.red));
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_two, 0, 0);
					tvTab1.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(position_one,
							position_two, 0, 0);
					tvTab2.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three,
							position_two, 0, 0);
					tvTab4.setTextColor(resources.getColor(R.color.black));
				}
				tvTab3.setTextColor(resources.getColor(R.color.red));
				break;
			case 3:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_three, 0, 0);
					tvTab1.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(position_one,
							position_three, 0, 0);
					tvTab2.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two,
							position_three, 0, 0);
					tvTab3.setTextColor(resources.getColor(R.color.black));
				}
				tvTab4.setTextColor(resources.getColor(R.color.red));
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);
			animation.setDuration(300);
			ivBottomLine.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	public void goBack(View view) {
		finish();
	}
}
